package com.smart.message.sender;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.smart.message.MessageProducer;

/**
 * 
 * @author de
 *
 */
@Service
public class StringMessageSender implements MessageSender {

	private final Logger logger = LoggerFactory.getLogger(StringMessageSender.class);

	private final static String TAG = "SmartTag";

	private final static String KEYS = "SmartKeys";

	@Resource
	private MessageProducer messageProducer;

	@Override
	public String sendMessage(String topic, String keys, Object obj) {
		if (StringUtils.isEmpty(keys)) {
			keys = KEYS;
		}

		Message msg = new Message(topic, // topic
				TAG, // tag
				keys, // key
				obj.toString().getBytes());// body
		SendResult sendResult;
		try {
			sendResult = messageProducer.getDefaultMQProducer().send(msg);
			logger.info("sendResult:" + sendResult);
			if (sendResult != null) {
				return sendResult.getMsgId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";

	}

}
